package com.zld.smarket.controller;

import com.zld.smarket.pojo.Goods;
import com.zld.smarket.pojo.Stock;
import com.zld.smarket.pojo.Stockin;
import com.zld.smarket.result.ExceptionMsg;
import com.zld.smarket.result.ResponseData;
import com.zld.smarket.service.StockService;
import com.zld.smarket.service.StockinService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.util.Date;

@RestController
@CrossOrigin
@RequestMapping("/stockin")
public class StockinController {
    @Autowired
    private StockinService stockinSerinvice;

    @Autowired
    private StockService stockSerinvice;

    @RequestMapping(value = "/list",method = RequestMethod.GET)
    public ResponseData findList(Integer page, Integer limit){
        return  stockinSerinvice.findList(page,limit);
    }

    @RequestMapping(value = "/search",method = RequestMethod.GET)
    public ResponseData search(String s,Integer page, Integer limit){
        return  stockinSerinvice.search(s,page,limit);
    }

    @RequestMapping(value = "/weekstockin",method = RequestMethod.GET)
    public Long weekstockin(){
        return  stockinSerinvice.weekstockin();
    }



    @RequestMapping(value = "/add")
    public ResponseData add(Stockin stockin){
        Long i=stockin.getStockinNo();
        if(stockinSerinvice.selectbyno(i)>0){
            return new ResponseData(ExceptionMsg.ADDERROR);
        }else {
            stockin.setStockinDate(new Date());
            if (stockSerinvice.selectbyGoodsno(stockin.getGoodsNo(),stockin.getSupplierId(),
                    stockin.getStorehouseNo()).size() == 1){

                Integer integer1 =stockSerinvice.addStockNum(stockin.getGoodsNo(),
                        stockin.getSupplierId(),stockin.getStorehouseNo(),stockin.getStockinNum());

                Integer integer = stockinSerinvice.addStockin(stockin);
                return new ResponseData(ExceptionMsg.SUCCESS);
            }else  {
                Stock stock=new Stock();
                stock.setId(null);
                stock.setGoodsNo(stockin.getGoodsNo());
                stock.setStockNum(stockin.getStockinNum());
                stock.setSupplierId(stockin.getSupplierId());
                stock.setStorehouseNo(stockin.getStorehouseNo());
                stock.setStockNumofwarn(30L);
                if(stockin.getStockinNum()>45L){
                    stock.setStockState(0L);
                }else if(stockin.getStockinNum()<=45L&&stockin.getStockinNum()>30L)
                {
                    stock.setStockState(1L);
                }else{stock.setStockState(2L);}
                Integer integer1 = stockSerinvice.addStock(stock);
                Integer integer = stockinSerinvice.addStockin(stockin);
                return new ResponseData(ExceptionMsg.SUCCESS);
            }
        }
    }

}
